package com.enqualcomm.kids.mvp.pedometer;

import android.content.Context;

import com.enqualcomm.kids.R;
import com.enqualcomm.kids.network.socket.response.QueryStepDayListResult;
import com.enqualcomm.kids.network.socket.response.StepItem;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

/**
 * Created by Sunzhigang on 2016/3/9.
 */
public class CalculateStepsUtil {

    public static Map<String,Integer> getStepsDayMap(Context context,QueryStepDayListResult.Data stepDayList) {
        if (stepDayList == null || stepDayList.daylist == null || stepDayList.daylist.size() == 0) {
            return null;
        }
        Map<String, Integer> stepsDayMap = new HashMap<>();
//        StepItem item = null;
//        int maxItem = 0; //步数最多的项
//        int[] progress = new int[8];
//        String[] desc = new String[7];
//        List<String> weaks = new ArrayList<>();
//        List<Integer> steps = new ArrayList<>();
//        for (int i = 0; i < stepDayList.daylist.size(); i++) {
//            item = stepDayList.daylist.get(i);
//            item.index = getIndex(item.time);//根据日期计算它应该处在的位置
//            if (item.index < 0 || item.index > 6) {//有可能缓存的数据，不在这七天之内
//                continue;
//            }
//            progress[item.index] = item.stepcount;
//            stepsDayMap.put(getweaks(item.time), item.stepcount);
//            desc[item.index] = String.valueOf(item.stepcount);
//            if (item.stepcount > maxItem) {
//                maxItem = item.stepcount;
//            }
//        }

        Calendar calendar = Calendar.getInstance();
        for (int i = 0,size = stepDayList.daylist.size(); i < size; i++) {
            StepItem stepItem = stepDayList.daylist.get(i);
            String[] time = stepItem.time.split("-");
            calendar.set(Integer.parseInt(time[0]), Integer.parseInt(time[1])-1, Integer.parseInt(time[2]));
            int week = calendar.get(Calendar.DAY_OF_WEEK);

            stepsDayMap.put(getWeekStr(context,week),stepItem.stepcount);
        }

        return stepsDayMap;
    }

    public static String getWeekStr(Context context,int cweek){
        String week = "";
        switch (cweek) {
            case 1:
                week = context.getString(R.string.week_sunday);
                break;
            case 2:
                week = context.getString(R.string.week_monday);
                break;
            case 3:
                week = context.getString(R.string.week_tuesday);
                break;
            case 4:
                week = context.getString(R.string.week_wednesday);
                break;
            case 5:
                week = context.getString(R.string.week_thursday);
                break;
            case 6:
                week = context.getString(R.string.week_firday);
                break;
            case 7:
                week = context.getString(R.string.week_saturday);
                break;
        }
        return week;
    }


    private static String getweaks(String date) {
        String[] split = date.split("-");
        Calendar calendar = Calendar.getInstance();
        calendar.set(Integer.parseInt(split[0]), Integer.parseInt(split[1]) - 1, Integer.parseInt(split[2]));
        long timeInMillis = calendar.getTimeInMillis();

        return getWeek(timeInMillis);
    }

    public static String getWeek(long millis) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(millis);
        String week = "";
        int cweek = calendar.get(Calendar.DAY_OF_WEEK);
        week = getWeek(cweek, false);
        return week;
    }

    private static String getWeek(int cweek, boolean iscaculate) {
        String week = "";
        if (cweek - 1 == 0 && iscaculate) {
            cweek = 1;
        } else if (cweek <= 0 && iscaculate) {
            cweek = cweek + 7;
        }
        switch (cweek) {
            case 1:
                week = "周日";
                break;
            case 2:
                week = "周一";
                break;
            case 3:
                week = "周二";
                break;
            case 4:
                week = "周三";
                break;
            case 5:
                week = "周四";
                break;
            case 6:
                week = "周五";
                break;
            case 7:
                week = "周六";
                break;
        }
        return week;
    }

    private static int getIndex(String date) {
        String[] split = date.split("-");
        Calendar calendar = Calendar.getInstance();
        calendar.set(Integer.parseInt(split[0]), Integer.parseInt(split[1]) - 1, Integer.parseInt(split[2]));
        long timeInMillis = System.currentTimeMillis() - calendar.getTimeInMillis();

        return 6 - (int) timeInMillis / 3600 / 1000 / 24;
    }

    public static int getCurrentDaySteps(Context context,Map<String, Integer> stepsDayMap) {
        Calendar calendar = Calendar.getInstance(Locale.getDefault());
        int steps = 0;
        int cweek = calendar.get(Calendar.DAY_OF_WEEK);


        if (stepsDayMap != null) {
            Integer integer = stepsDayMap.get(getWeekStr(context,cweek));
            if (integer!=null){
                steps = integer.intValue();
            }
        }
        return steps;
    }
}
